package main

import (
	"encoding/json"
	"flag"
	"jkgo/jk/jkhttp"
	"jkgo/jk/jklog"
	"strings"
	"time"
)

type ReportTools struct {
}
type ReportData struct {
	CreateTime int64    `bson:"createTime"`
	Id         string   `bson:"id"`
	Cmd        string   `bson:"cmd"`
	Class      string   `bson:"class"`
	Label      []string `bson:"label"`
	Attr       string   `bson:"attr"`
}

func ReportToolsNew(id, class string, label []string, attr string) error {
	rd := ReportData{
		CreateTime: time.Now().Unix(),
		Id:         id,
		Cmd:        "report",
		Class:      class,
		Label:      label,
		Attr:       attr,
	}
	d, err := json.Marshal(rd)
	if err != nil {
		return err
	}

	jklog.L().Infoln("send data ", string(d))
	r, err := jkhttp.NewJKHttpQuery("https://www.jmdkina.cn/report?cmd=report&code=ssclientadd", string(d))
	if err != nil {
		return err
	}
	d, err = r.Post()
	if err != nil {
		return err
	}
	jklog.L().Debugln(d)
	return nil
}

var (
	id    = flag.String("id", "", "id")
	class = flag.String("class", "", "class")
	label = flag.String("label", "", "label []string")
	attr  = flag.String("attr", "", "attr")
)

func main() {
	jklog.L().InitLog("reportTools")
	jklog.L().SetLevel(jklog.LEVEL_ALL)
	flag.Parse()
	labels := strings.Split(*label, " ")
	err := ReportToolsNew(*id, *class, labels, *attr)
	if err != nil {
		jklog.L().Errorln(err)
		return
	}
	jklog.L().Infoln("report success")
}
